package com.julian.adapterPattern;

import java.util.Random;

/**
 * 双向适配器
 */
public class DuAdapter implements Turkey, Duck{
    Duck duck;
    Random rand;
    Turkey turkey;
    public DuAdapter(Duck duck) {
        this.duck = duck;
        rand = new Random();
    }

    public DuAdapter(Turkey turkey) {
        this.turkey = turkey;
        rand = new Random();
    }

    @Override
    public void gobble() {
        if (turkey != null){
            turkey.gobble();
        }
    }

    @Override
    public void quack() {
        if (duck != null){
            duck.quack();
        }
    }

    @Override
    public void fly() {
        if(duck != null &&rand.nextInt(5) == 0){
            duck.fly();
        }else if (turkey != null){
            turkey.fly();
        }
    }
}
